package com.example.steveaoblogdemo.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.steveaoblogdemo.entity.Articles;
import com.example.steveaoblogdemo.entity.Category;
import com.example.steveaoblogdemo.entity.Tag;
import com.example.steveaoblogdemo.mapper.CategoryMapper;
import com.example.steveaoblogdemo.service.CategoryService;
import com.example.steveaoblogdemo.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Resource
    CategoryMapper categoryMapper;

    public Result<?> queryCategory(){
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",0);
        List<Category> list = categoryMapper.selectList(queryWrapper);
        return new Result<>(200,"查询成功",list);
    }

    public Result<?> query(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String search){
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(search)){
            queryWrapper.eq("status",0).like("c_name",search);
        }
        queryWrapper.eq("status",0);
        IPage<Category> categoryIPage = categoryMapper.selectPage(new Page<>(pageNum,pageSize),queryWrapper);
        return new Result<>(200,"查询成功",categoryIPage);
    }

    public int addCategory(@RequestParam Integer id,@RequestParam String cName){
        categoryMapper.addCategory(id,cName);
        return 0;
    }

    public int updateCategory(@RequestParam String cName,@RequestParam Integer id){
        categoryMapper.updateCategory(cName,id);
        return 0;
    }

    public Result<?> deleteCategory(@RequestParam Integer id){
        UpdateWrapper<Category> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id).set("status",1);
        int result = categoryMapper.update(null,wrapper);
        return new Result<>(200,"删除成功",result);
    }

    public Result<?> queryById(@RequestParam Integer id){
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        Category category = categoryMapper.selectOne(wrapper);
        if (category!=null){
            return new Result<>(200,"查询成功",category);
        }
        return new Result<>(400,"查询失败",null);
    }

    public Result<?> queryByCategory(@RequestParam Integer id){

        List<Articles> articles = categoryMapper.query(id);
        if (articles.size()>0){
            return new Result<>(200,"查询成功",articles);
        }
        return new Result<>(400,"查询失败",null);
    }
}

